package org.lisy.java.json;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonTransform {
	
	private static ObjectMapper objectMapper = new ObjectMapper();

	public static void main(String[] args) {
		// 测试科学计数法
		testObjectMapperE();
		
		System.out.println("========================================");
		
		Persion persion = null;
		String msg = null;
		
		persion = jsonObjectJsonStringToObject(null, persion);
		System.out.println("test jsonObjectJsonStringToObject string is null: " + persion);
		persion = objectMapperJsonStringToObject(null, persion);
		System.out.println("test objectMapperJsonStringToObject string is null: " + persion);
		
		System.out.println("========================================");
		
		msg = jsonObjectObjectToJsonString(null);
		System.out.println("test jsonObjectObjectToJsonString object is null: " + msg);
		msg = objectMapperObjectToJsonString(null);
		System.out.println("objectMapperObjectToJsonString object is null: " + msg);
		
		System.out.println("========================================");
		
		persion = new Persion("lisy", 20.5);
		System.out.println(persion.toString());
		
		System.out.println("========================================");
		
		msg = jsonObjectObjectToJsonString(persion);
		System.out.println("jsonObjectObjectToJsonString: " + msg);
		msg = objectMapperObjectToJsonString(persion);
		System.out.println("objectMapperObjectToJsonString: " + msg);
		
		System.out.println("========================================");
		
		persion = jsonObjectJsonStringToObject(msg, null);
		System.out.println("jsonObjectJsonStringToObject: " + persion.toString());
		persion = objectMapperJsonStringToObject(msg, null);
		System.out.println("objectMapperJsonStringToObject: " + persion.toString());
		
		persion.toString();
	}
	
	@SuppressWarnings("unchecked")
	private static void testObjectMapperE() {
		try {
			Map<String, Object> map = null;
			String str = "{\"age\":764578474866.8101}";
			map = objectMapper.readValue(str, Map.class);
			System.out.println("testE:" + map);
			
			objectMapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
			
			map = objectMapper.readValue(str, Map.class);
			System.out.println("testE:" + map);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
	}

	private static String objectMapperObjectToJsonString(Object persion) {
		String msg = null;
		try {
			msg = objectMapper.writeValueAsString(persion);
		} catch (JsonProcessingException e) {
			System.out.println(e.toString());
		}
		return msg;
	}

	private static String jsonObjectObjectToJsonString(Object persion) {
		String msg = JSONObject.toJSONString(persion);
		return msg;
	}

	private static Persion objectMapperJsonStringToObject(String msg, Persion persion) {
		try {
			persion = objectMapper.readValue(msg, Persion.class);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return persion;
	}

	private static Persion jsonObjectJsonStringToObject(String msg, Persion persion) {
		JSONObject jsonObject = JSONObject.parseObject(msg);
		persion = JSONObject.toJavaObject(jsonObject,  Persion.class);
		return persion;
	}

	public static class Persion {
		
		String name;
		double age;
		
		public Persion () {
			super();
		}
		
		public Persion(String name, double age) {
			super();
			this.name = name;
			this.age = age;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public double getAge() {
			return age;
		}

		public void setAge(double age) {
			this.age = age;
		}

		@Override
		public String toString() {
			return "Persion [name=" + name + ", age=" + age + "]";
		}
		
	}
}